class TrajectoryServer extends Ice.Application
{
    
    public int run(String[] args)
    {
	Ice.ObjectAdapter adapter;
	Ice.ObjectPrx subscriber;
	Boolean persistence = true;

	Ice.Properties properties = communicator().getProperties();
	String normality_topic_name = properties.getProperty("normality_topic");
	String aux1 = properties.getProperty("persistence");
	String aux2 = properties.getProperty("camera");

	if (aux1.equals("no"))
	    persistence = false;

	adapter = communicator().createObjectAdapter("TrajectoryServerAdapter");
	subscriber = adapter.add(new TAI(adapter.getCommunicator(), persistence),
				 communicator().stringToIdentity("ta"));

	System.out.println(".....");
	System.out.println("Subscribing with the event channel...");
	System.out.println(".....");

	IceGrid.QueryPrx query = IceGrid.QueryPrxHelper.checkedCast(communicator().stringToProxy("IceGrid/Query"));
	IceStorm.TopicManagerPrx manager = IceStorm.TopicManagerPrxHelper.checkedCast(query.findObjectById(communicator().stringToIdentity("Oculus.IceStorm/TopicManager")));

	//
        // Retrieve the topic.
        //
        IceStorm.TopicPrx topic;
        try {
	    topic = manager.retrieve(normality_topic_name);
	}
        catch(IceStorm.NoSuchTopic e) {
            try {
		topic = manager.create(normality_topic_name);
	    }
            catch(IceStorm.TopicExists ex) {
                System.err.println(appName() + ": temporary failure, try again.");
                return 1;
            }
        } // Fin catch NoSuchTopic.
	
	java.util.Map<String, String> qos = new java.util.HashMap<String, String>();
	qos.put("reliability", "ordered");

	try {
	    topic.subscribeAndGetPublisher(qos, subscriber);
	    System.out.println(subscriber.toString() + " is now subscribed!");
	}
        catch(IceStorm.AlreadySubscribed e) {
	    System.out.println(subscriber.toString() + " was already subscribed!");
        }
        catch(IceStorm.BadQoS e) {
            e.printStackTrace();
        }

	adapter.activate();

	communicator().waitForShutdown();

	return 0;
    }

    public static void main(String[] args)
    {
	TrajectoryServer ta = new TrajectoryServer();
	int status = ta.main("TrajectoryServer", args);

	System.exit(status);
    }

}
